package im.oen.boot.api.dispatch.handler;

import im.oen.boot.api.annotation.handler.MethodAroundHandler;
import im.oen.boot.api.dao.AbsDao;
import im.oen.boot.api.entity.AbsEntity;
import im.oen.boot.common.utils.JsonUtil;

import java.io.Serializable;

/**
 * @author Kevin.Xu
 */
public class DeleteMethodHandler implements MethodHandler {
    @Override
    public String cmd() {
        return "delete";
    }

    @Override
    public <T extends AbsEntity, ID extends Serializable> Object handler(AbsDao<T, ID> dao, MethodAroundHandler<T> methodAroundHandler, Class<T> entity, String[] paths, String body) {
        boolean emptyHandler = methodAroundHandler == null;
        T obj = JsonUtil.safeToObj(body, entity);
        if (!emptyHandler) {
            methodAroundHandler.deleteBefore(obj);
        }
        dao.delete(obj);
        if (!emptyHandler) {
            methodAroundHandler.deleteAfter(obj);
        }
        return obj;
    }
}
